using System;
using System.Collections.Generic;
using System.Text;

namespace BookLookupPlugin
{
    public class AmazonLookup
    {
        public enum SearchCriteria
        {
            Title = 0,
            Author = 1
        }

        public SearchResults[] SearchForBook(SearchCriteria criteria, string queryText)
        {
            // What it does: Search for a book based on criteria specified and query text

            // Check for null querytext
            if (queryText.Length == 0)
            {
                throw new Exception("You must enter the text you would like to query.");
            }

            SearchResults[] results = null;

            try
            {
                // Create new instance of Amazon.com eCommerce Web Service
                wsAmazon.AWSECommerceService service = new wsAmazon.AWSECommerceService();
                wsAmazon.ItemSearch itemSearch = new wsAmazon.ItemSearch();

                // Create ItemSearch Request
                wsAmazon.ItemSearchRequest request = new wsAmazon.ItemSearchRequest();
                itemSearch.SubscriptionId = "0525E2PQ81DD7ZTWTK82";

                // Set ResponseGroup details to return
                request.ResponseGroup = new string[] { "Images", "Small" };

                // Set Search Parameters
                request.SearchIndex = "Books";

                switch (criteria)
                {
                    case (SearchCriteria.Author):
                        request.Author = queryText;
                        break;
                    case (SearchCriteria.Title):
                        request.Title = queryText;
                        break;
                }

                // Setup Request
                itemSearch.Request = new wsAmazon.ItemSearchRequest[] { request };

                // Get the Response
                wsAmazon.ItemSearchResponse response = service.ItemSearch(itemSearch);
                if (response == null)
                {
                    throw new Exception("Server Error...No Response Received.");
                }

                wsAmazon.Items[] itemsArray = response.Items;

                // Check for errors, stop at first error...
                if (itemsArray == null)
                {
                    throw new Exception("Server Error ... Empty Response!");
                }
                if (itemsArray[0].Request.Errors != null)
                {
                    throw new Exception(itemsArray[0].Request.Errors[0].Message);
                }

                // Response Group Images
                foreach (wsAmazon.Items items in itemsArray)
                {
                    if (items != null)
                    {
                        Console.WriteLine("Total Pages: {0}", items.TotalPages);
                        Console.WriteLine("Total Results: {0}", items.TotalResults);
                    }

                    // Items:Item
                    wsAmazon.Item[] itemArray = items.Item;

                    // Create class that will return the data
                    results = new SearchResults[itemArray.Length];
                    int iResult = 0;

                    if (itemArray != null)
                    {
                        foreach (wsAmazon.Item item in itemArray)
                        {
                            if (item != null)
                            {

                                // Create new result instance
                                results[iResult] = new SearchResults();

                                Console.WriteLine("=== Item ===");

                                // Item:Image
                                wsAmazon.Image image = item.SmallImage;

                                if (image != null)
                                {
                                    // Image URL
                                    Console.WriteLine("Image URL: {0}", image.URL);
                                    results[iResult].ImageUrl = image.URL;
                                }

                                // ASIN Number
                                Console.WriteLine("ASIN: {0}", item.ASIN);
                                
                                // Details Page URL
                                Console.WriteLine("Detail Page URL: {0}", item.DetailPageURL);
                                results[iResult].DetailsUrl = item.DetailPageURL;

                                // Item:ItemAttributes
                                wsAmazon.ItemAttributes itemAttributes = item.ItemAttributes;

                                if (itemAttributes != null)
                                {
                                    // Mfg.
                                    Console.WriteLine("Manufacturer: {0}", itemAttributes.Manufacturer);

                                    // Product Group
                                    Console.WriteLine("Product Group: {0}", itemAttributes.ProductGroup);

                                    // Title
                                    Console.WriteLine("Title: {0}", itemAttributes.Title);
                                    results[iResult].Title = itemAttributes.Title;

                                    //Console.WriteLine("=== ItemAttributes ===");

                                    //// Actor
                                    //string[] actorArray = itemAttributes.Actor;

                                    //if (actorArray != null)
                                    //{
                                    //    foreach (string actor in actorArray)
                                    //    {
                                    //        Console.WriteLine("Actor: " + actor);
                                    //    }
                                    //}

                                    //// Artist
                                    //string[] artistArray = itemAttributes.Artist;

                                    //if (artistArray != null)
                                    //{
                                    //    foreach (string artist in artistArray)
                                    //    {
                                    //        Console.WriteLine("Artist: " + artist);
                                    //    }
                                    //}

                                    // Author
                                    string[] authorArray = itemAttributes.Author;

                                    if (authorArray != null)
                                    {
                                        foreach (string author in authorArray)
                                        {
                                            Console.WriteLine("Author: " + author);

                                            if (authorArray.Length > 1)
                                            {
                                                results[iResult].Author += author + ", ";
                                            }
                                            else
                                            {
                                                results[iResult].Author = author;
                                            }
                                        }
                                        // Trim "," at end of authors
                                        results[iResult].Author = results[iResult].Author.Trim().TrimEnd(Convert.ToChar(","));
                                    }

                                    // ItemAtrributes: Creator
                                    //wsAmazon.ItemAttributesCreator[] creatorArray = itemAttributes.Creator;

                                    //if (creatorArray != null)
                                    //{
                                    //    foreach (wsAmazon.ItemAttributesCreator creator in creatorArray)
                                    //    {
                                    //        if (creator != null)
                                    //        {
                                    //            Console.WriteLine("Role: {0}", creator.Role);
                                    //            Console.WriteLine("Value: {0}", creator.Value);
                                    //        }
                                    //    }
                                    //}

                                    // Director
                                    //string directorArray = itemAttributes.Director;

                                    //if (directorArray != null)
                                    //{
                                    //    foreach (string director in directorArray)
                                    //    {
                                    //        Console.WriteLine("Director: " + director);
                                    //    }
                                    //}
                                }
                            }
                            // Increment the resultset
                            iResult ++;
                        }
                    }
                }

                // Return the results
                return results;
            }
            catch
            {
                throw;
            }
        }
    }
}
